package cn.itcast.service.impl;

import cn.itcast.constant.JwtClaimsConstant;
import cn.itcast.context.BaseContext;
import cn.itcast.mapper.ShoppingCartMapper;
import cn.itcast.properties.JwtProperties;
import cn.itcast.service.ShoppingCartService;
import cn.itcast.utils.JwtUtil;
import cn.itheima.feign.DishClient;
import cn.itheima.feign.SetmealClient;
import cn.itheima.goods.pojo.Dish;
import cn.itheima.goods.vo.DishVO;
import cn.itheima.goods.vo.SetmealVO;
import cn.itheima.shoppingcart.dto.ShoppingCartDTO;
import cn.itheima.shoppingcart.pojo.ShoppingCart;
import io.jsonwebtoken.Claims;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@AllArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private ShoppingCartMapper shoppingCartMapper;
    private DishClient dishClient;
    private SetmealClient setmealClient;
    private HttpServletRequest httpServletRequest;
    private JwtProperties jwtProperties;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO 商品信息
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1、从请求头中获取令牌
        String token = httpServletRequest.getHeader(jwtProperties.getUserTokenName());
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //只能查询自己的购物车数据
        shoppingCart.setUserId(userId);

        //判断当前商品是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //如果已经存在，就更新数量，数量加1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            //如果不存在，插入数据，数量就是1

            //判断当前添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //添加到购物车的是菜品
                DishVO dish = dishClient.findById(dishId).getData();
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //添加到购物车的是套餐
                SetmealVO setmeal = setmealClient.findById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return 购物车商品列表
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //1、从请求头中获取令牌
        String token = httpServletRequest.getHeader(jwtProperties.getUserTokenName());
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());

        return shoppingCartMapper.list(ShoppingCart.builder().userId(userId).build());
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        //1、从请求头中获取令牌
        String token = httpServletRequest.getHeader(jwtProperties.getUserTokenName());
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());

        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO 商品信息
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId())
                .dishId(shoppingCartDTO.getDishId())
                .setmealId(shoppingCartDTO.getSetmealId())
                .dishFlavor(shoppingCartDTO.getDishFlavor()).build();

        //查询到当前商品的信息
        ShoppingCart cart = shoppingCartMapper.list(shoppingCart).get(0);

        if (cart.getNumber() > 1) {
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            shoppingCartMapper.deleteById(cart.getId());
        }
    }
}